<?php
	class SystemAction extends Action{
		
		public function userManage(){
			$teacher=M("Teacher");
			$all=$teacher->select();
			$count=count($all);
			for($i=0;$i<$count;$i++){
				$all[$i]["index"]=$i+1;
			}
			$this->assign("all",$all);
			echo $this->fetch();
		}
		public function userEditor($id="",$type="editor"){
			switch($type){
				case "editor":
					$teacher=M("Teacher");
					$one=$teacher->where("id=".$id)->select();
					$this->assign("one",$one[0]);
					$this->assign("id",$id);
					$this->display();
					break;
				case "new":
					$this->display();
					break;
				case "delete":
					$teacher=M("Teacher");
					$result=$teacher->where("id=".$id)->delete();
					if ($result>=0) {
						header("Content-type: text/html; charset=utf-8");
						echo '<center><font color="green">用户删除成功</center>';
					}else{
						echo '<center><font color="red">用户删除失败！！！</center>';
					}
					break;
				
			}
			if(isset($_POST["submit"])){
				$data["tid"]=$_POST["tid"];
				if (!empty($_POST["password"])) {
					$data["password"]=md5($_POST["password"]);
				}
				$data["name"]=$_POST["name"];
				$data["date"]=date("Y-m-d");
				$data["type"]=$_POST["type"];
				$teacher=M("Teacher");
				if (empty($_POST["id"])) {
					$result=$teacher->add($data);
				}else{
					$result=$teacher->where("id=".$_POST["id"])->save($data);
				}
				if ($result>=0) {
					header("Content-type: text/html; charset=utf-8");
					echo '<center><font color="green">信息编辑成功</center>';
				}else{
					header("Content-type: text/html; charset=utf-8");
					echo '<center><font color="red">信息编辑失败！！！</center>';
				}
			}
			
		}
		public function setting(){
			$code=M("Codetoname");
			$where["type"]="academy";
			$academy=$code->where($where)->select();
			$this->assign("academy",$academy[0]);
			echo $this->fetch();
		}
		public function changeSetting($type=""){
			$code=M("Codetoname");
			switch ($type){
				case "academy":
					$data["code"]=(int)$_POST["code"];
					$data["name"]=$_POST["name"];
					$where["type"]="academy";
					$result=$code->where($where)->save($data);
					if ($result>=0) {
						echo "院系修改成功";
					}else{
						echo "院系修改失败";
					}
					break;
				case "major":
					$data["name"]=$_POST["name"];
					$where["type"]="major";
					$where["code"]=(int)$_POST["code"];
					$count=$code->where($where)->count();
					if($count>0){
						$result=$code->where($where)->save($data);
						if ($result>=0) {
							echo "专业修改成功";
						}else{
							echo "专业修改失败";
						}
					}else{
						$data["code"]=(int)$_POST["code"];
						$data["type"]="major";
						$result=$code->add($data);
						if ($result>=0) {
							echo "专业创建成功";
						}else{
							echo "专业创建失败";
						}
					}
					
					break;
				case "class":
					$data["name"]=$_POST["name"];
					$where["type"]=$_POST["itype"];
					$where["code"]=(int)$_POST["code"];
					$count=$code->where($where)->count();
					if($count>0){
						$result=$code->where($where)->save($data);
						if ($result>=0) {
							echo "班级修改成功";
						}else{
							echo "班级修改失败";
						}
					}else{
						$data["code"]=(int)$_POST["code"];
						$data["type"]=$_POST["itype"];
						$result=$code->add($data);
						if ($result>=0) {
							echo "班级创建成功";
						}else{
							echo "班级创建失败";
						}
					}
					break;
				case "delete":
					$where["code"]=(int)$_POST["code"];
					$where["type"]=$_POST["itype"];
					$result=$code->where($where)->delete();
					if ($result>=0) {
						echo "删除成功";
					}else{
						echo "删除失败";
					}
					break;
			}
		}
	}
?>